package ModelLayer;

import java.text.SimpleDateFormat;

/**
 * Write a description of class Loan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Loan
{
    private int loanId;
    private long borrowDate;
    private int period;
    private String state;
    private Person loaner;
    private Copy copy;
   
    public Loan(int personID, int dvdID, int copySerialNr, int loanId, long borrowDate, int period, String state)
    {
        this.loanId = loanId;
        this.borrowDate = borrowDate;
        this.period = period;
        this.state = state;
        loaner = AddressBook.getInstance().getPerson(personID);        
        copy = DVDRecords.getInstance().getDVD(dvdID).getCopy(copySerialNr);
    }    
    
    public void setId(int loanId)
    {
        this.loanId = loanId;
    }
    
    public int getId()
    {
        return loanId;
    }
    
    public void setBorrowDate(long borrowDate)
    {
        this.borrowDate = borrowDate;
    }
    
    public long getBorrowDate()
    {
        return borrowDate;
    }
    
    public void setBorrowPeriod(int period)
    {
        this.period = period;
    }
    
    public int getBorrowPeriod()
    {
        return period;
    }
   
    public void setState(String state)
    {
        this.state = state;
    }
    
    public String getState()
    {
        return state;
    }
    
    public String returnState(long returnDate)
    {
        if(((borrowDate + (period * 86400000))) - returnDate < 0)
        {
            setState("Expired but returned");
        } 
        else 
        {
            setState("Returned");
        }
        getCopy().setSts("Available");
        return state;
    }    
    
    public void setPerson(int personID)
    {
        loaner = AddressBook.getInstance().getPerson(personID);
    }
    
    public Person getLoaner()
    {
        return loaner;
    }
    
    public void setCopy(int dvdID, int copySerialNr)
    {
        copy = DVDRecords.getInstance().getDVD(dvdID).getCopy(copySerialNr);
    }
    
    public Copy getCopy()
    {
        return copy;
    }
    
    public void printLoanInfo()
    {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        System.out.println(" Borrow to person(person id): " + loaner.getId());
        System.out.println(" Borrow DVD: " + copy.getDVD().getId());
        System.out.println(" Borrow copy: " + copy.getSerialNumber());
        System.out.println(" Borrow date: " + formatter.format(borrowDate));
        System.out.println(" Borrow period: " + period + " days");
        System.out.println(" State: " + state);
        System.out.println("");
    }
}
